
$(document).ready(function() {

        //log('editAttributes.js: document.ready')

        /*
        * Ukoliko uredjujemo vlasnika, kroz ucitani html dosupne su promjenjljive:
        * 
        * var company_person;
        * var tax_type;
        * var citizenship;
        * var sex;
        * var language;
        * var country;
        * 
        */     
        editOwnerDialog = function(url,title)
        {
            var url = url;
            var dialogClass = 'editOwnerDialog';
            var dialogTitle = title;
            var formToSubmit = '#editOwnerDialog_form';
            var buttons = [
                {
                    id: 'editOwnerButton',
                    text: "OK",
                    click: function() { jQuery(formToSubmit).trigger('submit'); }
                }
            ];            
            openNewDialogOnOpen = function() {

                jQuery(formToSubmit).validate({

                        /*rules: {
                        'owner_type[]': {required :true},                     
                        surname: "required",                                                               
                        tax_type: { check_item_dropdown: true }
                        }, */

                        highlight: function(element) {

                            console.log(element.name)

                            if(element.id=='email' || element.name=='company_person' || element.name=='owner_type[]'){
                                $(element).parent().parent().parent().addClass("error");
                            }else{
                                $(element).parent().parent().addClass("error");
                            }

                        },
                        unhighlight: function(element) {

                            if(element.id=='email' || element.name=='company_person' || element.name=='owner_type[]'){
                                $(element).parent().parent().parent().removeClass("error");
                            }else{
                                $(element).parent().parent().removeClass("error");
                            }

                        },

                        submitHandler: function(form) {

                            //disable add button                            
                            $('#'+buttons[0].id).button("option", "disabled", true);
                            $('.'+dialogClass+' .status_info').html(loader_html());


                            var form = $(formToSubmit);
                            var form_data = form.serialize();

                            $.ajax({
                                    url: base_url+'owners/submit_post',
                                    dataType: 'json',
                                    type: "POST",
                                    data: form_data,
                                    success: function (data, textStatus, xhr) {

                                        jQuery('#ajax-loading').css('visibility','hidden');

                                        if(data.action==true){

                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'success'));

                                            function action_delay(){
                                                $('.'+dialogClass).dialog("close");  
                                            }
                                            window.setTimeout(function() { action_delay(); }, 1001);

                                        }else{

                                            $('#'+buttons[0].id).button("option", "disabled", false);
                                            $('.'+dialogClass+' .status_info').html(alert_html(data.msg,'error'));
                                            $('.'+dialogClass).animate({
                                                    scrollTop: $('.'+dialogClass+' .status_info').offset().top
                                                }, 200);
                                        }
                                    },
                                    error: function (xhr, textStatus, errorThrown) {
                                        //enable add button                            
                                        $('#'+buttons[0].id).button("option", "disabled", false); 
                                        $('.'+dialogClass+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                                    }
                            });

                        },
                        errorPlacement: function(error, element){}

                });

                switch (myOwner[0]['mode']) {
                    case 'NEW':
                        select_owner_type('company');
                        $("#tax_type").val(0);
                        $("#citizenship").val(0);
                        $("#sex").val(0);
                        $("#language").val(0);
                        $("#country").val(0);
                        break;
                    case 'EDIT':
                        myOwner[0]['type'] = company_person;
                        select_owner_type(myOwner[0]['type']);
                        $("#tax_type").val(tax_type);
                        $("#citizenship").val(citizenship);
                        $("#sex").val(sex);
                        $("#language").val(language);
                        $("#country").val(country);
                        break;
                    default:
                        alert('myOwner javascript object mode is undefined');
                }


            }
            var DialogOnOpen = openNewDialogOnOpen;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,1223,$(window).height()-40)
        }

        //Todo:programaticly select radio
        select_owner_type = function(type)
        {
            $('#radio').buttonset();
            switch(type)
            {
                case 'company':
                    $('.cg-company_ID').show();
                    $('.cg-personal_ID').hide();
                    $('.cg-birthdate').hide();
                    $('.cg-pasport_number').hide();
                    $('.cg-sex').hide();
                    $('.cg-surname').hide();
                    $('.cg-birthplace').hide();
                    $('.cg-citizenship').hide();
                    $('[name="company_person"][value="company"]').attr("checked", true); 

                    //log('company')
                    break;

                case 'person':
                    $('.cg-company_ID').hide();
                    $('.cg-personal_ID').show();
                    $('.cg-birthdate').show();
                    $('.cg-pasport_number').show();
                    $('.cg-sex').show();
                    $('.cg-surname').show();
                    $('.cg-birthplace').show();
                    $('.cg-citizenship').show();
                    $('#company_person-person').attr('checked', true);
                    $('[name="company_person"][value="person"]').attr("checked", true); 

                    //log('person')
                    break;
            }

            $('#radio').buttonset("refresh");

        }

        $('[name="company_person"]').live('change',function(){
                select_owner_type(this.value);
        });

        //fill form with some data
        $('#fill_form').live('click',function(){

                log('fill')

                // General
                $("#company_person-person").attr('checked','checked');
                $("#owner_customer").attr('checked','checked');
                $("#owner_suplier").attr('checked','checked');
                $("#name").val('ivan');
                $("#surname").val('kukic');
                $("#personal_ID").val('123456');
                $("#tax_type").val('Local tax');
                $("#birthdate").val('01.08.1982');                
                $("#birthplace").val('Banjaluka');
                $("#pasport_number").val('AC37B6V');
                $("#citizenship").val('montenegrin');
                $("#sex").val('male');
                $("#language").val('Montenegrin');

                // Contact data
                $("#address").val('Jovana Tomaševića');
                $("#city").val('Bar');
                $("#zip_code").val('85000');
                $("#country").val('Montenegro');
                $("#email").val('ikukic@yahoo.com');
                $("#mobile_phone").val('069 069 982');
                $("#telephone").val('312 324');
                $("#telephone2").val('312 324');
                $("#telefax").val('312 324');

                // Bank
                $("#bank_name").val('NLB Montenegro');
                $("#bank_ac_number").val('530-20322-60');
                $("#swift").val('u38s00sj');

                // Other details
                $("#contract_type").val('1 year');
                $("#default_option_period").val('123');
                $("#accounting_code").val('321');
                $("#contract_nr").val('001-12');
                $("#due_date").val('432');

        })

        openNewOwner = function()
        {
            myOwner[0]['ID'] = null;  
            myOwner[0]['mode'] = 'NEW';
            editOwnerDialog(myOwner[0]['formViewUrl'],'New Owner');
        }

        openEditOwner = function (id)
        {
            myOwner[0]['ID'] = null;  
            myOwner[0]['mode'] = 'EDIT';
            editOwnerDialog(myOwner[0]['formEditUrl']+id,'Edit Owner');
        }

})